package com.ss.android.action.b;

import android.os.SystemClock;
import android.text.TextUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.app.AbsApplication;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class ImpressionRecorder implements b {
    public interface a {
        void c();
    }

    final class g$b {
        public String a;
        public int b;
        public long c;
        public long d;
        public long e;
        public String f;
        public String g;
        public long h;
        public String i;
        public int j;
        public String k;
        public int l;
        public int m;

        g$b(byte arg1) {
            this();
        }

        private g$b() {

        }
    }

    public final int a;
    public final String b;
    public String c;
    private final Map d;
    private final a e;

    public ImpressionRecorder(int arg2, String arg3, String arg4, a arg5) {
        this.d = new HashMap();
        this.a = arg2;
        this.b = arg3;
        this.e = arg5;
        this.c = arg4;
    }

    public ImpressionRecorder(String arg2, a arg3) {

        this.d = new HashMap();
        this.a = 1;
        this.b = arg2;
        this.e = arg3;
    }

    public final JSONArray a(boolean arg15) {
        long v2;
        JSONObject v9;
        Object v0_1;
        long v4;
        JSONArray v1 = new JSONArray();
        try {
            long v6 = SystemClock.elapsedRealtime();
            v4 = System.currentTimeMillis();
            Iterator v8 = this.d.values().iterator();
            while(true) {
            label_7:
                if(!v8.hasNext()) {
                    break;
                }
                v0_1 = v8.next();
                if(((g$b)v0_1).c <= 0) {
                    continue;
                }

                v9 = new JSONObject();
                v9.put("id", ((g$b)v0_1).a);
                v9.put("type", ((g$b)v0_1).b);
                if(v6 > ((g$b)v0_1).c) {
                    v2 = v4 - (v6 - ((g$b)v0_1).c);
                } else {
                    v2 = v4;
                }
                v9.put("time", v2 / 1000);
                if(((g$b)v0_1).d > 0) {
                    v9.put("duration", ((g$b)v0_1).d);
                }

                if(((g$b)v0_1).e > 0 && ((g$b)v0_1).e != ((g$b)v0_1).d) {
                    v9.put("max_duration", ((g$b)v0_1).e);
                }

                if(((g$b)v0_1).f != null) {
                    v9.put("value", ((g$b)v0_1).f);
                }

                if(!android.support.design.a.isTextEmpty(((g$b)v0_1).g)) {
                    v9.put(((g$b)v0_1).g, ((g$b)v0_1).h);
                }

                if(!android.support.design.a.isTextEmpty(((g$b)v0_1).i)) {
                    v9.put(((g$b)v0_1).i, ((g$b)v0_1).j);
                }

                if(!android.support.design.a.isTextEmpty(((g$b)v0_1).k)) {
                    v9.put("log_extra", ((g$b)v0_1).k);
                }

                if(((g$b)v0_1).m > 0 && ((g$b)v0_1).l > 0) {
                    v9.put("style", ((g$b)v0_1).l);
                    v9.put("sub_style", ((g$b)v0_1).m);
                }

                v1.put(v9);
          continue;
            }
        }
        catch(Exception v0) {
        }

        if(arg15) {
            this.d.clear();
        }

        return v1;
    }

    private void a(com.ss.android.action.b.a arg9, boolean arg10) {
        int v0_2;
        if(arg9 != null && (!arg10 || (arg9.a()))) {
            if(Logger.debug()) {
                String v1 = "ImpressionRecorder";
                StringBuilder v2 = new StringBuilder();
                String v0 = arg10 ? "resume" : "pause";
                Logger.d(v1, v2.append(v0).append("AllImpression ").append(this.b).toString());
            }

            List v4 = arg9.b();
            if(v4 == null) {
                return;
            }

            if(v4.isEmpty()) {
                return;
            }

            int v5 = v4.size();
            int v2_1 = 0;
            int v1_1;
            for(v1_1 = 0; v2_1 < v5; v1_1 = v0_2) {
                Object v0_1 = v4.get(v2_1);
                if(v0_1 != null) {
                    if((arg10) && !arg9.a(v2_1, ((FeedViewHolder)v0_1))) {
                        ((FeedViewHolder)v0_1).a = 0;
                        v0_2 = v1_1;
                        ++v2_1;
                        continue;
                    }

                    v1_1 = 1;
                    if(arg10) {
                        this.resumeImpression(((FeedViewHolder)v0_1), false);
                        v0_2 = 1;
                        ++v2_1;
                        continue;
                    }

                    this.pauseImpression(((FeedViewHolder)v0_1));
                    v0_2 = v1_1;
                } else {
                    v0_2 = v1_1;
                }

                ++v2_1;
            }

            if(v1_1 == 0) {
                return;
            }

            if(this.e == null) {
                return;
            }

            this.e.c();
        }
    }

    private void resumeImpression(FeedViewHolder arg7, boolean arg8) {
        if(arg7 != null && arg7.d != null) {
            if(Logger.debug()) {
                Logger.d("ImpressionRecorder", "resumeImpression " + arg7.d + " " + this.b + " invokeHook " + arg8);
            }

            long v0 = SystemClock.elapsedRealtime();
            if(arg7.a > 0) {
                if(v0 >= arg7.a && v0 - arg7.a < 1000) {
                    return;
                }

                if(Logger.debug()) {
                    Logger.v("ImpressionRecorder", "try to resume un-paused impression: " + arg7.itemId + " " + arg7.b + " " + arg7.a);
                }
            }

            arg7.a = v0;
            if(!arg8) {
                return;
            }

            if(this.e == null) {
                return;
            }

            this.e.c();
        }
    }

    public final void a(com.ss.android.action.b.a arg2) {
        this.a(arg2, true);
    }

    public final void a(FeedViewHolder arg2) {
        this.resumeImpression(arg2, true);
    }

    public final void a(String arg9, String arg10, String arg11, long arg12, String arg14, int arg15) {
        long v6 = 0;
        String v1 = "1_" + arg9;
        long v2 = SystemClock.elapsedRealtime();
        if(this.d.get(v1) == null) {
            g$b v0 = new g$b((byte) 0);
            v0.a = arg10;
            v0.b = 1;
            v0.g = arg11;
            v0.h = arg12;
            v0.i = arg14;
            v0.j = arg15;
            v0.c = v2;
            v0.d = v6;
            v0.e = v6;
            this.d.put(v1, v0);
        }
    }

    public final void b(com.ss.android.action.b.a arg2) {
        this.a(arg2, false);
    }

    public final void b(FeedViewHolder arg1) {
        this.pauseImpression(arg1);
    }

    private void pauseImpression(FeedViewHolder arg15) {
        long v4;
        long v12 = 0;
        if(arg15 != null && arg15.d != null && arg15.a > v12) {
            if(Logger.debug()) {
                Logger.d("ImpressionRecorder", "pauseImpression " + arg15.d + " " + this.b);
            }

            long v2 = SystemClock.elapsedRealtime();
            g$b v0_1 = (g$b) this.d.get(arg15.d);
            if(v0_1 == null) {
                v0_1 = new g$b((byte) 0);
                v0_1.a = arg15.itemId;
                v0_1.b = arg15.b;
                v0_1.c = v2;
                v0_1.d = v12;
                v0_1.e = v12;
                v0_1.f = arg15.e;
                v0_1.g = arg15.f;
                v0_1.h = arg15.groupItemId;
                v0_1.i = arg15.h;
                v0_1.j = arg15.aggrType;
                v0_1.k = arg15.logExtra;
                v0_1.l = arg15.category;
                v0_1.m = arg15.style;
                this.d.put(arg15.d, v0_1);
            }

            v2 -= arg15.a;
            long v10 = v2 < v12 ? v12 : v2;
            if(arg15.c()) {
                v0_1.l = 0;
                v0_1.m = 0;
            }

            if((arg15.c()) && v10 > v12) {
                JSONObject v8 = new JSONObject();
                try {
                    v8.put("duration", v10);
                    try {
                        if(!TextUtils.isEmpty(arg15.feedCallAppAdLogExtra)) {
                            v8.put("log_extra", arg15.feedCallAppAdLogExtra);
                        }

                        v4 = Long.valueOf(arg15.adId).longValue();
                        MobAdClickCombiner.onAdEvent(AbsApplication.getApplication(), "embeded_ad", "show_over", v4, 0, v8, 2);
                    } catch(Exception v1_1) {
                        try {
                            v8.put("value", arg15.adId);
                            v4 = v12;
                            MobAdClickCombiner.onAdEvent(AbsApplication.getApplication(), "embeded_ad", "show_over", v4, 0, v8, 2);
                        } catch(JSONException v1) {
                            v1.printStackTrace();
                        }
                    }
                }
                catch(JSONException v1) {
                    v1.printStackTrace();
                }
            }

            if(v0_1.e < v10) {
                v0_1.e = v10;
            }

            v0_1.d += v10;
            arg15.a = v12;
        }
    }
}

